/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package plar.demo;

import java.io.*;

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import plar.ClientServer.Server;
import plar.core.Common;
import plar.core.Level;
import java.awt.Point;

public class server  extends javax.swing.JFrame {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		   Common.info(1,Common.product+" ver:"+Common.version);
	       Common.info(1,"Created by "+Common.author+" "+Common.copyright);
	       Common.info(1,Common.license);
		boolean silent=false;
		Server s = new Server();
				
		boolean loadedLevel=false;
		
		String response= JOptionPane.showInputDialog(null,
			"Welcome to "+Common.serverName+"\r\nSet the platform level [ defautlt: Level0 ]", 
			"Plar",  JOptionPane.QUESTION_MESSAGE);		
			
		if(response.equals("")) response="Level0";
		response="res.levels."+response;
		Level l=null;
		int error=0;
		
	   try {
		   Common.info(1,"!");
                Class c = Class.forName(response ) ;
                Common.info(1,"!");
                l = (Level)c.newInstance() ;
                Common.info(1,"!");
                //l=(Level)l;
				
        } catch ( Exception e ) {
                error = 1;
                //System.exit(0);
        }
        catch(java.lang.NoClassDefFoundError e)
        {
			error=2;
		}
        if(error==1){
				JOptionPane.showMessageDialog(null,
				"Failed loading the class "+response, "Plar",
				JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
        //if(l!=null)
	}if(error==2){
				JOptionPane.showMessageDialog(null,
				"Failed loading JBox2D, be sure you have "+Common.jarFile,"Plar",
				JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
        //if(l!=null)
	}
				s.newGame(l);
				s.loadGame();
		
		
		
		//s.g.resolution = new Point(800,600);
		for(int i=0;i<args.length;i++)
		{
			if(args[i].equals("--noGui")) silent=true;
			//Common.info("["+args[i]+"]");
		}
		if(s.listen(Common.port))
		{
			if(!silent)
			{
			/*JOptionPane.showMessageDialog(null,
				"The server is started.\r\nClick OK to STOP the server running and QUIT. ", "Plar",
				JOptionPane.WARNING_MESSAGE);
			s.close();*/
		  java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new server().setVisible(true);}});
		}
		
		}else
		{
			s.close();
			System.exit(0);
		}

	}
	 public server() {
        initComponents();
	}
       private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        JLabel l = new JLabel("    Server is running..    ");
        
		getContentPane().add(l);
		
		//setSize(50,50);
		
		//l.setSize(l.getWidth()+10,l.getHeight()+10);
		
pack();
setSize(l.getWidth(),l.getHeight()+50);
        
    }
    
    


}
